package com.ajax.servlet;

import com.ajax.pojo.Student;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wzm
 * @create 2023-11-22 15:17
 * project_name:web2310
 * @Description
 */
@WebServlet("/findAll")
public class FindAllServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //模拟查询全部数据
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/json;charset=UTF-8");

        //调用servcice查询全部数据
        //模拟查询到的集合
        List<Student> list = new ArrayList<>();
        list.add(new Student(1,"张三1",18,"昆明"));
        list.add(new Student(2,"张三2",18,"昆明"));
        list.add(new Student(3,"张三3",18,"昆明"));
        list.add(new Student(4,"张三4",18,"昆明"));
        list.add(new Student(5,"张三5",18,"昆明"));
        list.add(new Student(6,"张三6",18,"昆明"));

        //java=>字符串响应
        String jsonString = JSON.toJSONString(list);

        //响应到客户端
        resp.getWriter().write(jsonString);


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }
}
